<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@ page language="java" %>
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta HTTP-EQUIV="EXPIRES" CONTENT="0">
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

   <script>
      // Retrieves the list of companies for a searched company name/desc
      function search()
      {
         fnForward('company.shs', 'initCompanyList');
      }

      // Retrieves the details for a selected company
      function view(companyId)
      {
         Form.companyId.value = companyId;
         fnForward('company.shs', 'getCompanyDetails');
      }

      // Brings up the screen to add a new companys
      function add() 
      {
         fnForward("company.shs", "addCompany");
      }

      // De-activates/Activates selected companies
      function fnToggleStatus(status) 
      {
         Form.active.value = status;
         fnForward("company.shs", "updActiveStatus");
      }

      // After the document loads, need to do the colouring of rows
      $(document).ready(function() {
         $('.listingTbl tr:even').addClass("evenRow");
         $('.listingTbl tr:odd').addClass("oddRow");
      })
   </script>
</head>
<body class="textInputNormal_12">

   <form name="companyForm" method="post">
      <input type="hidden" name="impl"/>
      <input type="hidden" name="companyId"/>
      <input type="hidden" name="active"/>
      
      <script>
         var pgName = "custCtr";
         var Form = document.companyForm;
      </script>
      <div>
         Search for company: 
         <input type="text" name="companySrch" class="textInputNormal_12"
            value="${companyForm.companySrch}"/>
         &nbsp;
         <input type="submit" class="bttn" value=" Go " onclick="search()"/>
         <input type="button" class="bttn" value="  Add  " onclick="add()" id="addBttn"/>
         &nbsp;
         <input type="checkbox" name="srchInactive" value="1"
            <c:if test="${companyForm.srchInactive == 1}">checked</c:if>/>
         <label class='textNormal_12'>Search de-activated companies too</label>
      </div>
      
      <c:choose>
         <c:when test="${success != null}">
            <div class="successDiv" style="margin-top: 10px;">
               ${success}
            </div>
         </c:when>
         <c:when test="${failure != null}">
            <div class="errDiv" style="margin-top: 10px;">
               ${failure}
            </div>
         </c:when>
      </c:choose>
      
      <c:choose>
         <c:when test="${companyList != null && not empty(companyList)}">
            <div style="margin-top: 10px;">
               The different companies registered with the system are listed below:
               <div class="scrollBox" style="width: 70%">               
                  <div class="mainListing">
                     <table width="100%" class="listingTbl">
                        <tr class="header">
                           <td width="3%">
                              <input type="checkbox" name="chkMain" onclick=""/>
                           </td>
                           <td width="40%">
                              Company Name
                           </td>
                           <td width="45%">
                              City
                           </td>
                           <td>
                              Is active?
                           </td>
                        </tr>
                        <%-- 
                           The following logic iterates through the list of companies
                           and displays the corresponding fields
                        --%>
                        <c:forEach items="${companyList}" var="company">
                           <tr>
                              <td width="3%">
                                 <input type="checkbox" name="chkCompanyId" value="${company.companyId}"/>
                              </td>
                              <td width="40%">
                                 <a href="#" class="gLink" onclick="view('${company.companyId}')"> 
                                    ${company.companyName}
                                 </a>
                              </td>
                              <td width="45%">
                                 ${company.city}
                              </td>
                              <td style="text-align: center; vertical-align: middle; ">
                                 <c:set var="state"/>
                                 <c:set var="stateTitle"/>
                                 <c:choose>
                                    <c:when test="${company.active == true}">
                                       <c:set var="state" value="active"/>
                                       <c:set var="stateTitle" value="Company is active. De-activate?"/>
                                    </c:when>
                                    <c:otherwise>
                                       <c:set var="state" value="inactive"/>
                                       <c:set var="stateTitle" value="Company is inactive. Activate?"/>
                                    </c:otherwise>
                                 </c:choose>
                                 <img src="data/images/${state}.png" title="${stateTitle}" style="cursor: pointer;"/>
                              </td>
                           </tr>
                        </c:forEach>
                     </table>
                  </div>
               </div>
            </div>
            <input type="button" class="bttn" value="De-activate" onclick="fnToggleStatus('0')" id="deactivateBttn"/>
            &nbsp;
            <input type="button" class="bttn" value="Activate" onclick="fnToggleStatus('1')" id="activateBttn"/>
         </c:when>
         <c:otherwise>
            <div class="errDiv" style="margin-top: 15px;">
               No Company registered with the system.
            </div>
         </c:otherwise>
      </c:choose>
   </form>

</body>
</html>